package cn.itcast.java.inputstream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author itcast
 * Date 2021/10/13 16:09
 * 将读取input.dat文件的数据将其保存到 output.dat 文件中
 * 1.读取出来 input.dat 文件 FileInputStream
 * <p>
 * 2.写入到 output.dat 文件中
 */
public class FileInputStreamDemo {
    public static void main(String[] args) {
        //实例化
        try {
            FileInputStream fis = new FileInputStream("data/input.dat");
            //定义输出流
            FileOutputStream fos =  new FileOutputStream("data/output.dat");
            /*int read = fis.read();
            System.out.println(read);

            int read2 = fis.read();
            System.out.println(read2);

            int read3 = fis.read();
            System.out.println(read3);*/
            int len = 0;
            byte[] bts = new byte[3];  // 1024
                while ((len = fis.read(bts)) != -1) {
                //new String(bts) 将字节数组转换成字符串
                // 0 : offset 从哪里开始
                // length : 当前读到的有效的字节的长度
                System.out.println(new String(bts,0,len));
                fos.write(bts,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
